{{ header }}{{ column_left }}
<div id="content">
	<div class="page-header">
		<div class="container-fluid">
			<div class="float-end"><button type="button" id="button-upload" data-bs-toggle="tooltip" title="{{ button_upload }}" data-oc-loading-text="{{ text_loading }}" class="btn btn-primary"><i class="fas fa-upload"></i></button></div>
			<h1>{{ heading_title }}</h1>
			<ol class="breadcrumb">
				{% for breadcrumb in breadcrumbs %}
					<li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
				{% endfor %}
			</ol>
		</div>
	</div>
	<div class="container-fluid">
		<div class="card">
			<div class="card-header"><i class="fas fa-puzzle-piece"></i> {{ heading_title }}</div>
			<div class="card-body">
				<fieldset>
					<legend>{{ text_progress }}</legend>
					<div class="row mb-3">
						<label class="col-sm-2 col-form-label">{{ entry_progress }}</label>
						<div class="col-sm-10 mt-2">
							<div class="progress">
								<div id="progress-bar" class="progress-bar" style="width: 0%;"></div>
							</div>
							<div id="progress-text"></div>
						</div>
					</div>
				</fieldset>
				<fieldset>
					<legend>{{ text_installed }}</legend>
					<div id="extension">{{ list }}</div>
				</fieldset>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript"><!--
$('#extension').on('click', 'thead a, .pagination a', function(e) {
    e.preventDefault();

    $('#extension').load(this.href);
});

$('#extension').load('index.php?route=marketplace/installer|extension&user_token={{ user_token }}');

// Upload
$('#button-upload').on('click', function() {
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    $('#form-upload input[name=\'file\']').on('change', function() {
        if (this.files[0].size > {{ config_file_max_size }}) {
            $(this).val('');

            alert('{{ error_upload_size }}');
        }
    });

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {
        if ($('#form-upload input[name=\'file\']').val() != '') {
            clearInterval(timer);

            $.ajax({
                url: 'index.php?route=marketplace/installer|upload&user_token={{ user_token }}',
                type: 'post',
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $('#button-upload').button('loading');
                },
                complete: function() {
                    $('#button-upload').button('reset');
                },
                success: function(json) {
                    if (json['error']) {
                        alert(json['error']);
                    }

                    if (json['success']) {
                        alert(json['success']);

                        $('#extension').load('index.php?route=marketplace/installer|extension&user_token={{ user_token }}');
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
    }, 500);
});

// Install
$('#extension').on('click', '.btn-success, .btn-warning', function(e) {
    e.preventDefault();

    var element = this;

    $('#progress-bar').removeClass('progress-bar-danger progress-bar-success').css('width', '0%');
    $('#progress-text').html('');

    $.ajax({
        url: $(element).attr('href'),
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#progress-bar').addClass('progress-bar-danger');
                $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['success']) {
                $('#progress-bar').addClass('progress-bar-success').css('width', '100%');
                $('#progress-text').html('<div class="text-success">' + json['success'] + '</div>');

                $('#extension').load('index.php?route=marketplace/installer|extension&user_token={{ user_token }}');
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Delete
$('#extension').on('click', '.btn-danger', function(e) {
    e.preventDefault();

    var element = this;

    if (confirm('{{ text_confirm }}')) {
        $.ajax({
            url: $(element).attr('href'),
            dataType: 'json',
            beforeSend: function() {
                $(element).button('loading');
            },
            complete: function() {
                $(element).button('reset');
            },
            success: function(json) {
                $('.alert-dismissible').remove();

                if (json['error']) {
                    $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
                }

                if (json['success']) {
                    $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                    $('#extension').load('index.php?route=marketplace/installer|extension&user_token={{ user_token }}');
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
});
//--></script>
{{ footer }}
